
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_favourite_groups.
	''' </summary>
	Public Partial Class view_favourite_groups
		Inherits BaseWebPage

		Protected litGroupsCount As System.Web.UI.WebControls.Literal
		Protected blankImagesCount As Integer = 0
		Protected currentPageNumber As Integer = 0
		Protected firstPageNumber As Integer = 0
		' the first page of page links
		Protected lastPageNumber As Integer = 0
		Protected pagesRest As Integer = 0
		Protected usrID As Integer = -1

		Protected favGroups As FavoriteGroups = Nothing

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			' this.pageTemplate.PageTitle = PageTitles.VIEW_FAVORITE_GROUPS_PAGE;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.VIEW_FAVORITE_GROUPS, "", _Form1, Nothing, Nothing)
			Me.initAdvertisements(_Form1, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			favGroups = usr.FavoriteGroups
			Dim favGroupsCount As Double = Convert.ToDouble(favGroups.Count)
			If favGroupsCount = 0 Then
				lblNoFavGroups.Visible = True
				tblFavGroups.Visible = False

				tblFavGroups.Visible = False
				lblNoFavGroups.Visible = True
			Else
				dgPager.RedirectPage = Constants.VIEW_FAVORITE_GROUPS
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(favGroupsCount / Constants.MAX_GRID_RESULTS_ON_PAGE))

				dgPagerUp.RedirectPage = Constants.VIEW_FAVORITE_GROUPS
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(favGroupsCount / Constants.MAX_GRID_RESULTS_ON_PAGE))

				dgPager.Action = ""
				dgPagerUp.Action = ""
				' initialize listing literals

				dgPager.dgListing.TotalCount = Convert.ToInt32(favGroupsCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(favGroupsCount)

				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = Convert.ToString(((dgPager.CurrentPageNumber - 1) * Constants.MAX_GRID_RESULTS_ON_PAGE))
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				If Not IsPostBack Then
					BindGrid()
				End If
			End If
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim usr As BO.User = Me.LoggedUser

			Dim dt As New DataTable()

			Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
			Dim endIndex As Integer = startIndex + Constants.MAX_GRID_RESULTS_ON_PAGE
			Dim t As Integer = 0
			Dim counter As Integer = 0
			Dim pic As GroupPicture
			endIndex = Math.Min(endIndex, Convert.ToInt32(favGroups.Count))
			t = (endIndex - startIndex)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("GroupID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("LongName", GetType(String)))
			dt.Columns.Add(New DataColumn("URL", GetType(String)))
			dt.Columns.Add(New DataColumn("GroupImg", GetType(String)))
			dt.Columns.Add(New DataColumn("Category", GetType(String)))
			dt.Columns.Add(New DataColumn("ShortDescription", GetType(String)))
			dt.Columns.Add(New DataColumn("ModeratorName", GetType(String)))
			dt.Columns.Add(New DataColumn("ProfileUpdated", GetType(String)))

			dt.Columns.Add(New DataColumn("SendMessageLink", GetType(String)))
			dt.Columns.Add(New DataColumn("ForwardToFriendLink", GetType(String)))
			dt.Columns.Add(New DataColumn("AddCommentsLink", GetType(String)))
			dt.Columns.Add(New DataColumn("UnsubscribeLink", GetType(String)))
			dt.Columns.Add(New DataColumn("UnsubscribeLinkVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("JoinLink", GetType(String)))
			dt.Columns.Add(New DataColumn("JoinLinkVisibility", GetType(Boolean)))

			' List search results
			Dim curGroupUpdateDate As DateTime

			For Each favGrp As FavoriteGroup In favGroups
				Dim result As Group = favGrp.Group

				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()
					curGroupUpdateDate = result.LastEditedDate
					dr(0) = result.ID
					dr(1) = getBeginning(HttpUtility.HtmlEncode(result.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					dr(2) = result.Name
					dr(3) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + result.ID
					pic = result.DefaultPicture
					If pic <> Nothing Then
						dr(4) = Pictures.GetPictureSrcFullURL(pic, True, "../")
					Else
						dr(4) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
					End If

					dr(5) = Constants.CATEGORIES.GetCategoryByID(result.CatID).Name
					dr(6) = Server.HtmlEncode(result.ShortAbout)
					dr(7) = Users.GetUserByID(result.Uid).Name
					dr(8) = Constants.MONTHS(curGroupUpdateDate.Month - 1) + " " + curGroupUpdateDate.Day + ", " + curGroupUpdateDate.Year + " " + curGroupUpdateDate.ToShortTimeString()

					'links

					dr(9) = "../" + Constants.SEND_MESSAGE + "?action=" + Constants.MESSAGE_TO_GROUP_ACTION + "&" + Constants.GROUP_PARAM + "=" + result.ID
					dr(10) = "../" + Constants.SEND_MESSAGE + "?" + Constants.FORWARD_A_GROUP_PARAM + "=" + result.ID + "&action=" + Constants.MESSAGE_FORWARD_A_GROUP_ACTION
					dr(11) = "../" + Constants.FRIENDS_ADD_COMMENT + "?usr=" + usr.ID + "&" + Constants.GROUP_PARAM + "=" + result.ID
					If result.Members.Contains(usr.ID) Then
						dr(12) = "../" + Constants.CONFIRM_UNSUBSCRIBE + "?" + Constants.GROUP_PARAM + "=" + result.ID
						dr(13) = True
						dr(15) = False
					Else
						dr(12) = ""
						dr(13) = False
						dr(15) = True
					End If
					dr(14) = "../" + Constants.CONFIRM_JOIN_GROUP + "?" + Constants.GROUP_PARAM + "=" + result.ID

					dt.Rows.Add(dr)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgFavGroups.DataSource = CreateDataSource()
			dgFavGroups.DataBind()
		End Sub

		Private Sub dgFavGroups_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim cmd As String = e.CommandName

			Select Case cmd
				Case "Remove"
					Dim usr As User = LoggedUser
					usr.FavoriteGroups.Remove(Convert.ToInt32(e.CommandArgument))
					Response.Redirect(Constants.VIEW_FAVORITE_GROUPS)
					Exit Select
				Case "MemberRedirect"
					Response.Redirect(Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + e.CommandArgument)
					Exit Select
			End Select
		End Sub

		Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
			Dim stringBuilder As New StringBuilder()
			Dim stringWriter As New StringWriter(stringBuilder)
			Dim htmlWriter As New HtmlTextWriter(stringWriter)
			MyBase.Render(htmlWriter)
			Dim html As String = stringBuilder.ToString()
			Dim start As Integer = html.IndexOf("<form name=""") + 12
			Dim [end] As Integer = html.IndexOf("""", start)
			Dim formID As String = html.Substring(start, [end] - start)
			Dim replace As String = formID.Replace(":", "_")
			html = html.Replace("document." + formID, "document." + replace)
			writer.Write(html)
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgFavGroups.ItemCommand, AddressOf Me.dgFavGroups_ItemCommand
		End Sub
#End Region

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable _Form1.
		====================
		Public ReadOnly Property _Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated__Form1
			End Get
		End Property
		'set { Migrated__Form1 = value; }
	End Class
End Namespace






